package com.platform.config.interceptor;

import com.alibaba.fastjson.JSON;
import com.platform.license.client.LicenseVerify;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liujizheng
 * @version 1.0
 * @ClassName LicenseCheckInterceptor
 * @Description 拦截器
 * @date 2022/1/14 11:18
 */
public class LicenseCheckInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LicenseVerify licenseVerify = new LicenseVerify();

        //校验证书是否有效
        if (licenseVerify.verify()) {
            return true;
        }

        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        Map<String, String> result = new HashMap<>(1);
        result.put("result", "您的证书无效，请核查服务器是否取得授权或重新申请证书！");
        response.getWriter().write(JSON.toJSONString(result));

        return false;
    }
}
